package com.drg.center.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.drg.center.entity.param.SysRoleListDTO;
import com.guaike.core.mybatis.plugins.BaseDAO;
import com.drg.center.entity.SysRolePO;
import com.drg.center.mapper.SysRoleMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

/**
 * @Author: zhangxilin
 * @Date: 2024/5/21
 */
@Repository
public class SysRoleDAO extends BaseDAO<SysRoleMapper, SysRolePO> {

    public IPage<SysRolePO> findPage(SysRoleListDTO param){
        LambdaQueryWrapper<SysRolePO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(StringUtils.isNotBlank(param.getRoleName()), SysRolePO::getRoleName, param.getRoleName());
        lambdaQueryWrapper.orderByDesc(SysRolePO::getCreateTime);
        return getBaseMapper().selectPage(new Page<>(param.getPageIndex(), param.getPageSize()), lambdaQueryWrapper);
    }

    public boolean isExist(String name, Long excludeId){
        LambdaQueryWrapper<SysRolePO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysRolePO::getRoleName, name);
        lambdaQueryWrapper.ne(excludeId !=null, SysRolePO::getId, excludeId);
        return getBaseMapper().selectCount(lambdaQueryWrapper)>0? true: false;
    }

}
